package fang.controller;

import fang.pojo.MyStudent;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.*;


@Controller
public class BodyController {

    @RequestMapping("/body/text")
    public String body(Model model) {
        model.addAttribute("msg", "后端开发<b>java</b><b>mySql</b>");
        model.addAttribute("content", "全栈开发<b>vue</b><b>java</b><b>mySql</b>");
        return "body";
    }

    @RequestMapping("/body/each")
    public String each(Model model) {
        ArrayList<MyStudent> list = new ArrayList<>();
        Collections.addAll(list,
                new MyStudent(1001, "张飞", 22),
                new MyStudent(1002, "关羽", 24),
                new MyStudent(1003, "黄忠", 29),
                new MyStudent(1004, "马超", 30),
                new MyStudent(1005, "安琪拉", 16));
        model.addAttribute("students", list);

        Map<String, Object> stuMap = new HashMap<>();
        stuMap.put("学号", 1001);
        stuMap.put("姓名", "方瑞川");
        stuMap.put("年龄", 22);
        stuMap.put("班级", "Java高级一班");
        model.addAttribute("stuMap", stuMap);

        // List-Map
        ArrayList<Map<String, MyStudent>> listMap = new ArrayList<>();
        MyStudent s1 = new MyStudent(1001, "汪汪", 18);
        MyStudent s2 = new MyStudent(1002, "小智", 22);
        Map<String, MyStudent> map1 = new HashMap<>();
        map1.put("s-1-1", s1);
        map1.put("s-1-2", s2);
        listMap.add(map1);
        MyStudent s3 = new MyStudent(1003, "小小", 18);
        MyStudent s4 = new MyStudent(1004, "后来", 24);
        MyStudent s5 = new MyStudent(1005, "王先生", 17);
        map1 = new HashMap<>();
        map1.put("s-2-1", s3);
        map1.put("s-2-2", s4);
        map1.put("s-2-3", s5);
        listMap.add(map1);
        model.addAttribute("listMap", listMap);

        // []
        String names[] = {"刘备", "曹操", "孙权"};
        model.addAttribute("names", names);

        Map<Integer, String> cities = new HashMap<>();
        cities.put(001, "北京");
        cities.put(002, "上海");
        cities.put(003, "广州");
        cities.put(004, "杭州");
        cities.put(005, "苏州");
        model.addAttribute("cities", cities);
        model.addAttribute("choice", 004);
        return "each";
    }

    @RequestMapping("/body/if")
    public String doIf(Model model) {
        // true
        model.addAttribute("old", true);
        model.addAttribute("login", "login");
        model.addAttribute("str0", "");
        model.addAttribute("num1", -2);
        model.addAttribute("num2", 10);
        model.addAttribute("obj1", new MyStudent());

        // false
        model.addAttribute("young", false);
        model.addAttribute("str1", "off");
        model.addAttribute("str2", "no");
        model.addAttribute("str3", "false");
        model.addAttribute("num3", 0);
        model.addAttribute("obj2", null);

        return "if";
    }

    @RequestMapping("/body/tpl")
    public String doTpl(Model model) {
        model.addAttribute("tplname", "menu");
        return "frag/main";
    }

    @RequestMapping("/body/remove")
    public String remove(Model model) {
        return "remove";
    }

    @RequestMapping("/body/inline")
    public String doInline(Model model) {
        ArrayList<MyStudent> students = new ArrayList<>();
        MyStudent s1 = new MyStudent(1001, "刘备", 29);
        MyStudent s2 = new MyStudent(1002, "关羽", 20);
        MyStudent s3 = new MyStudent(1003, "张飞", 30);
        Collections.addAll(students, s1, s2, s3);
        model.addAttribute("students", students);
        model.addAttribute("name", "三国演义");
        model.addAttribute("info", "我是<b>HELLO</b>");
        return "inline";
    }

    @RequestMapping("/body/var")
    public String doVar(Model model) {
        model.addAttribute("age", 18);
        return "var";
    }

    @RequestMapping("/body/toolsobject")
    public String tools(Model model) {
        model.addAttribute("myDate", new Date());
        model.addAttribute("price", 89.35);
        model.addAttribute("name", "zhangsan");
        return "toolsobject";
    }

    @RequestMapping("/body/inobject")
    public String inObject(Model model, HttpSession session) {
        model.addAttribute("attrName", "我是request作用域中的数据attrName");
        session.setAttribute("sessionAttr", "我是session中的数据sessionAttr");
        session.getServletContext().setAttribute("contextAttr", "我是servletContext中的数据contextAttr");
        return "inobject";
    }
}
